package org.example;

import java.io.*;

public class 第三十四章_序列化 {
    public static void main(String[] args) throws Exception{
        P1 p1 = new P1();
        p1.name = "小明";
        p1.age = 18;

        //序列化
        FileOutputStream fileOut = new FileOutputStream("D:\\tmp\\p1.ser");
        ObjectOutputStream stream = new ObjectOutputStream(fileOut);
        stream.writeObject(p1);
        stream.close();
        fileOut.close();

        P1 p2;
        //反序列化
        FileInputStream fileInputStream = new FileInputStream("D:\\tmp\\p1.ser");
        ObjectInputStream stream1 = new ObjectInputStream(fileInputStream);
        p2 = (P1) stream1.readObject();
        stream1.close();
        fileInputStream.close();
        System.out.println(p2.toString());
    }

    static class P1 implements Serializable{
        public String name;
        public Integer age;

        public String toString(){
            return "姓名: "+name+",年龄: "+age;
        }
    }
}
